//
//  OrderDetailViewController.m
//  DuBai
//
//  Created by darktm on 14-3-19.
//  Copyright (c) 2014年 darktm. All rights reserved.
//

#import "OrderDetailViewController.h"


#import "RcvAdressViewController.h"

#import "UIImageView+WebCache.h"

#import "OrderDetail.h"
#import "TaskWithGetOrderDetail.h"
#import "TaskWithCommitOrder.h"

#import "Addresses.h"


@interface OrderDetailViewController ()<WYHttpTaskDelegate,RcvAdressViewControllerDelegate,UITextFieldDelegate>

@property (strong,nonatomic)OrderDetail *orderDetail;

@property(nonatomic,strong)MBProgressHUD* mb;

@end

@implementation OrderDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(on_keyboard_will_show:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(on_keyboard_will_hiden:) name:UIKeyboardWillHideNotification object:nil];
    
    
    self.txtMsg.layer.cornerRadius = 4.0f;
    self.txtMsg.layer.borderColor = [[[UIColor darkGrayColor] colorWithAlphaComponent:.3] CGColor];
    self.txtMsg.layer.borderWidth = 1.0f;
    
    self.txtScore.layer.cornerRadius = 4.0f;
    self.txtScore.layer.borderColor = [[[UIColor darkGrayColor] colorWithAlphaComponent:.3] CGColor];
    self.txtScore.delegate = self;
    self.txtScore.layer.borderWidth = 1.0f;
    
    
    [self loadOrderDetail];
}

#pragma mark - notification -

-(void)on_keyboard_will_show:(NSNotification *)n{
    
    
    CGRect frame;
    [n.userInfo[UIKeyboardFrameEndUserInfoKey] getValue:&frame];
    DLog(@"%.2f,",frame.size.height);
    
    
    UIScrollView *s = (UIScrollView *)self.view;
    
    [UIView animateWithDuration:.25 animations:^{
        s.contentOffset = CGPointMake(0, frame.size.height - 30);
    }];
    
}

-(void)on_keyboard_will_hiden:(NSNotification *)n{
    UIScrollView *s = (UIScrollView *)self.view;
    
    [UIView animateWithDuration:.25 animations:^{
        s.contentOffset = CGPointMake(0, 63);
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.txtMsg resignFirstResponder];
    [self.txtScore resignFirstResponder];
}


#pragma mark - textfiled delegate -

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if([textField isEqual:self.txtScore]){
        
        if(textField.text.length && string.length){
            textField.text = [textField.text stringByAppendingString:string];
        }else{
            textField.text = string;
        }
        
        int put = [textField.text intValue];
        int count = [self.orderDetail.grade intValue];
        int price = [self.orderDetail.price intValue];
        
        if(put >= count){
            put = count;
        }
        
        self.offsetMoney.text = [NSString stringWithFormat:@"可使用积分抵扣：%d 元",put];
        self.goodTotalMoney.text = [NSString stringWithFormat:@"%d",put + price];
        
        textField.text = [NSString stringWithFormat:@"%d",put];
        
        return NO;
    }
    
    
    
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - http delegate -

-(void)requestCompleteWithObj:(WYHttpTask *)obj{
    
    id rsult = [obj parese];
    
    if([obj isKindOfClass:[TaskWithGetOrderDetail class]]){
        self.orderDetail = rsult;
        [self refreshView];
    }else if([obj isKindOfClass:[TaskWithCommitOrder class]]){
        
    }
}
-(void)requestFailWithMsg:(WYHttpTask *)obj{
    DLog(@"%@",obj.networkOp.url);
    
    if([obj isKindOfClass:[TaskWithGetOrderDetail class]]){
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }else if([obj isKindOfClass:[TaskWithCommitOrder class]]){
        
    }
}

#pragma mark - customer method -

-(void)loadOrderDetail{
    TaskWithGetOrderDetail *detail = [[TaskWithGetOrderDetail alloc] init];
    detail.goodid = self.goodid;
    detail.delegate = self;
    [detail start];
}

-(void)refreshView{
    
//    self.self
    
    self.orderDetail.grade = @"22";
    
    self.orderNum.text = self.orderDetail.number;
    self.rcvName.text = self.orderDetail.name;
    self.mobile.text = self.orderDetail.mobile;
    self.phone.text = self.orderDetail.telephone;
    self.address.text = self.orderDetail.address;
    
    [self.goodImage setImageWithURL:[NSURL URLWithString:[IMAGE_BASE_URL stringByAppendingString:self.orderDetail.icon]]];
    self.goodDesc.text = self.orderDetail.desc;
    self.goodSale.text = self.orderDetail.name;
    self.goodPrice.text = self.orderDetail.price;
    self.goodSend.text = self.orderDetail.distribution;
    self.goodSendPrice.text = self.orderDetail.freight;
    self.goodAvailableScore.text = self.orderDetail.grade;
    self.offsetMoney.text = [NSString stringWithFormat:@"可使用积分抵扣：0 元"];
    self.goodTotalMoney.text = self.orderDetail.price;
    
    UIScrollView *s = (UIScrollView *)self.view;
    s.contentSize = CGSizeMake(0, 570);
}

#pragma mark - customer delegate -

-(void)rcvAdressDidSelectedInfo:(id)info{
    DLog(@"刷新地址信息：%@",info);
    Addresses *a = info;
}


#pragma mark - btn actions -

- (IBAction)on_btn_pay:(id)sender {
    
    self.mb = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    
    
}

- (IBAction)on_btn_change_address:(id)sender {
    RcvAdressViewController *rcv = (RcvAdressViewController *)[Utils storyBoardWithId:@"RcvAdressViewController"];
    rcv.delegate = self;
    [self.navigationController pushViewController:rcv animated:YES];
}

- (IBAction)on_btn_add_address:(id)sender {
}
@end
